<template>
  <div class="mod-index-img">
    <el-dialog
      :title="!dataForm.imgId ? $t('brand.add') : $t('coupon.edit')"
      :close-on-click-modal="false"
      :visible.sync="visible"
      @close="closeDialogHandle"
    >
      <el-form
        :model="dataForm"
        ref="dataForm"
        :rules="dataRule"
        :label-width="this.$i18n.t('language') === 'language' ? '160px' : '120px'"
      >
        <el-form-item :label="$t('platform.advertisingPic')" prop="pic">
          <img-upload v-model="dataForm.pic"></img-upload>
          <span v-if="dataForm.imgType === 0">{{$t('platform.recommImgSize')}}750*400</span>
          <span v-if="dataForm.imgType === 1">{{$t('platform.recommImgSize')}}1920*450</span>
        </el-form-item>
        <el-form-item :label="$t('platform.advertisingHeadline')" prop="headline">
          <el-input v-model="dataForm.headline" ></el-input>
        </el-form-item>

        <el-form-item :label="$t('platform.advertisingContent')" prop="content">
          <el-input v-model="dataForm.content" ></el-input>
        </el-form-item>

        <el-form-item :label="$t('platform.advertisingSkipUrl')" prop="skipUrl">
          <el-input v-model="dataForm.skipUrl" ></el-input>
        </el-form-item>

        <el-form-item :label="$t('brand.status')" prop="status">
          <el-radio-group v-model="dataForm.status">
            <el-radio :label="1">{{$t('publics.disable')}}</el-radio>
            <el-radio :label="0">{{$t('publics.normal')}}</el-radio>
          </el-radio-group>
        </el-form-item>

      </el-form>
      <span slot="footer">
      <div class="default-btn" @click="visible = false">{{$t("remindPop.cancel")}}</div>
      <div class="default-btn primary-btn" @click="dataFormSubmit()">{{$t("remindPop.confirm")}}</div>
    </span>
    </el-dialog>

  </div>
</template>

<script>
import ImgUpload from '@/components/img-upload'
import ProdsSelect from '@/components/prods-select'
import ProdPic from '@/components/prod-pic'
export default {
  data () {
    return {
      dataForm: {
        status: 0,
        pic: '',
        headline: '',
        content: '',
        skipUrl: '',
        imgType: 1,
        advertisingId: 0
      },
      isSubmit: false,
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      dataRule: {
        pic: [
          { required: true, message: this.$i18n.t('platform.carouselImgNoNull'), trigger: 'blur' }
        ],
        headline: [
          { required: true, message: this.$i18n.t('webInfoConfig.canNotBeEmpty'), max: 30, trigger: 'blur' }
        ],
        content: [
          { required: true, message: this.$i18n.t('webInfoConfig.canNotBeEmpty'), max: 180, trigger: 'blur' }
        ],
        skipUrl: [
          { required: true, message: this.$i18n.t('webInfoConfig.canNotBeEmpty'), max: 80, trigger: 'blur' }
        ]
      },
      prodsSelectVisible: false,
      visible: false
    }
  },
  components: {
    ImgUpload,
    ProdsSelect,
    ProdPic
  },
  methods: {
    // 获取分类数据
    init (id) {
      this.visible = true
      this.isSubmit = false
      this.dataForm.advertisingId = id || 0
      this.$nextTick(() => {
        this.prodsSelectVisible = false
        this.$refs['dataForm'].resetFields()
      })
      if (this.dataForm.advertisingId) {
        // 获取产品数据
        this.$http({
          url: this.$http.adornUrl(`/platform/advertising/info/${this.dataForm.advertisingId}`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({ data }) => {
          this.dataForm = data
        })
      } else {
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          this.dataForm.pic = ''
        })
      }
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (!this.dataForm.pic) {
          return
        }
        let param = this.dataForm
        if (this.isSubmit) {
          return false
        }
        this.isSubmit = true
        this.$http({
          url: this.$http.adornUrl(`/platform/advertising`),
          method: param.advertisingId ? 'put' : 'post',
          data: this.$http.adornData(param)
        }).then(({ data }) => {
          this.$message({
            message: this.$i18n.t('remindPop.success'),
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList', this.page)
            }
          })
        }).catch((e) => {
          this.isSubmit = false
        })
      })
    },
    // 关闭对话框回调
    closeDialogHandle () {
      this.prodsSelectVisible = false
      this.$refs['dataForm'].resetFields()
    }
  }
}
</script>
<style lang="scss" scoped>
.mod-index-img {
  .index-image-prod-card {
    position: relative;
    .card-prod-status-tips {
      position: absolute;
      right: 0;
      bottom: 54px;
      color: #fff;
      font-size: 12px;
      line-height: 16px;
      background-color:rgba(0, 0, 0, .3);
      padding: 3px 6px;
      border-top-left-radius: 4px;
    }
  }
}
.whether{
  padding-top: 10px;
  width: 100%;
  display: flex;
  justify-content:flex-end;
  border-top: 1px solid #eeeeee;

}
</style>
